'use strict';
exports.main = async (event, context) => {
  const { _id } = event;
  const db = uniCloud.database();
  const user = db.collection('post-data');

  try {
    // 1. 获取文档信息
    const doc = await user.doc(_id).get();
	
    if (!doc.data || !doc.data[0].images) {
      throw new Error('帖子不存在或缺少 fileID');
    }
    // 2. 从文档中提取 fileID
    const fileId = doc.data[0].images.map(item => item.url)

	await uniCloud.deleteFile({
		fileList: fileId
	})

    const res = await user.doc(_id).remove();

    return { message: '文档及关联图片已成功删除' };
  } catch (error) {
    return { error: '删除失败', details: error.message };
  }
};